﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using OrderManager.Domain;
using OrderManager.Domain.Entities;

namespace OrderManager.Web
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindCustomers();
                cBtnOrder.Visible = false;
            }
        }

        private void BindCustomers()
        {
            Session["Customers"] = DomainContext.Instance.RepositoryFactory.GetCustomerRepository().GetAll();
            cRptCustomers.DataSource = Session["Customers"];
            cRptCustomers.DataBind();
        }

        protected void OnCustomerSelect(object sender, CommandEventArgs e)
        {
            Session["CurrentCustomerID"] = Convert.ToInt32(Convert.ToString(e.CommandArgument));
            BindOrders((int)Session["CurrentCustomerID"]);

            foreach (RepeaterItem item in cRptCustomers.Items)
            {
                var customerId = Convert.ToInt32(((HiddenField)item.FindControl("cCustomerId")).Value);
                if (customerId == (int)Session["CurrentCustomerID"])
                {
                    ((LinkButton)item.FindControl("cLnkCustomerName")).Style.Add("font-weight", "bold");
                }
                else
                {
                    ((LinkButton)item.FindControl("cLnkCustomerName")).Style.Remove("font-weight");
                }
            }

        }

        private void BindOrders(int currentCustomerId)
        {
            Session["Orders"] =
                DomainContext.Instance.RepositoryFactory.GetOrderRepository().GetOrdersForCustomer(currentCustomerId);
            cRptOrders.DataSource = Session["Orders"];
            cRptOrders.DataBind();
        }

        protected void OnCustomerItemBound(object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    var customer = (Customer)e.Item.DataItem;

                    ((HiddenField)e.Item.FindControl("cCustomerId")).Value = customer.Id.ToString();

                    var cBtnCustomerName = (LinkButton)e.Item.FindControl("cLnkCustomerName");
                    cBtnCustomerName.CommandArgument = customer.Id.ToString();
                    cBtnCustomerName.Text = customer.Name;

                    var cLblCustomerAddress = (Label)e.Item.FindControl("cLblCustomerAddress");
                    cLblCustomerAddress.Text = customer.Address;

                    //var cBtnEditCustomer = (LinkButton)e.Item.FindControl("cBtnEditCustomer");
                    //cBtnEditCustomer.CommandArgument = customer.Id.ToString();
                    //cBtnEditCustomer.Text = "Edit";
                    
                    var cBtnDeleteCustomer = (LinkButton)e.Item.FindControl("cBtnDeleteCustomer");
                    cBtnDeleteCustomer.CommandArgument = customer.Id.ToString();
                    cBtnDeleteCustomer.Text = "Delete";

                    break;
            }
        }

        protected void OnOrdersItemBound(object sender, RepeaterItemEventArgs e)
        {
            cBtnOrder.Visible = true;
            switch (e.Item.ItemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    var order = (Order)e.Item.DataItem;

                    ((HiddenField)e.Item.FindControl("cOrderId")).Value = order.Id.ToString();

                    var cLblOrderNumber = (Label)e.Item.FindControl("cLbOrderNumber");
                    cLblOrderNumber.Text = order.Number.ToString();

                    var cLblOrderDate = (Label)e.Item.FindControl("cLblOrderDate");
                    cLblOrderDate.Text = order.OrderDate.GetDateTimeFormats('d')[0];

                    var cLblOrderAmount = (Label)e.Item.FindControl("cLblOrderAmount");
                    cLblOrderAmount.Text = order.Amount.ToString();

                    var cLblOrderDescription = (Label)e.Item.FindControl("cLblOrderDescription");
                    cLblOrderDescription.Text = order.Desctiption;

                    break;
            }
        }

        protected void cBtnAddCustomer_Click(object sender, EventArgs e)
        {
            Response.Redirect("FormsForAdd/CustomerAdd.aspx");
        }

        protected void DeleteCustomer(object sender, EventArgs e)
        {
            
        }

        protected void cBtnOrder_Click(object sender, EventArgs e)
        {
            Response.Redirect("FormsForAdd/OrderAdd.aspx");
        }

        protected void OnDeleteCustomer(object sender, CommandEventArgs e)
        {
            var currentCustomerId = Convert.ToInt32(Convert.ToString(e.CommandArgument));

            foreach (Customer customer in (List<Customer>)Session["Customers"])
            {
                if (customer.Id == currentCustomerId)
                {
                    customer.Delete();
                    ((List<Customer>)Session["Customers"]).Remove(customer);
                    cRptCustomers.DataSource = Session["Customers"];
                    cRptCustomers.DataBind();
                    BindOrders((int)Session["CurrentCustomerID"]);
                    break;
                }
            }
        }

        protected void OnEditCustomer(object sender, CommandEventArgs e)
        {
            //TODO add implementation
        }
    }
}